home *** CD-ROM | disk | FTP | other *** search
- unit ShutDownU;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls;
-
- type
- TForm1 = class(TForm)
- btnRestart: TButton;
- procedure btnRestartClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- {$ifdef Ver90} { Delphi 2.0x }
- {$define Delphi32LessThan3}
- {$endif}
- {$ifdef Ver93} { C++ Builder 1.0x }
- {$define Delphi32LessThan3}
- {$endif}
-
- {$ifdef Delphi32LessThan3}
- procedure Win32Check(Value: Boolean);
- begin
- if not Value then
- raise Exception.Create(SysErrorMessage(GetLastError))
- end;
- {$endif}
-
- procedure TForm1.btnRestartClick(Sender: TObject);
- var
- HToken: THandle;
- TP, OldTP: TTokenPrivileges;
- ReturnLen: Integer;
- begin
- if Win32Platform = VER_PLATFORM_WIN32_NT then
- begin
- //Get a token for this process
- Win32Check(OpenProcessToken(GetCurrentProcess(),
- TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
- {$ifdef Delphi32LessThan3}@{$endif}HToken));
- //Get the LUID for the shutdown privilege
- Win32Check(LookupPrivilegeValue(nil,
- 'SeShutdownPrivilege', TP.Privileges[0].Luid));
- TP.PrivilegeCount := 1; //One privilege to set
- TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
- //Acquire shutdown privilege for this process
- Win32Check(AdjustTokenPrivileges(HToken,
- False, TP, SizeOf(OldTP), OldTP, ReturnLen));
- end;
- //Shut down the system and force all applications to close
- if not ExitWindowsEx(EWX_SHUTDOWN {or EWX_FORCE}, 0) then
- raise Exception.Create('Cannot shut Windows');
- Close;
- end;
-
- end.
-